<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统基础管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row :gutter="10">
        <el-col span="4">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="adduser"
            >新增</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table
        stripe
        border
        ref="multipleTable"
        :data="userList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="id" label="id" width="120" align="center">
        </el-table-column>
        <el-table-column
          prop="userName"
          label="用户名"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="accountName"
          label="账号"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="roleName"
          label="所属角色"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="locked"
          label="帐号状态"
          width="160"
          align="center"
        >
        <template #default="scope">
            <span v-if="scope.row.locked == 1">可用</span>
            <span v-else-if="scope.row.locked == 0">禁用</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="site"
          label="挂靠站点"
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="时间"
          width="400"
          align="center"
        >
          <template #default="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center" fixed="right">
          <template #default="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editDialog(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="del(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNow"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="rowCount"
      >
      </el-pagination>
    </el-card>
    <!-- 添加对话框 -->
    <el-dialog title="新增用户" v-model="addDialogVisible" width="40%">
      <!-- 对话框内容 -->
      <el-form
        :model="addForm"
        status-icon
        :rules="addFormrules"
        ref="addFormRef"
        label-width="135px"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input
            v-model="addForm.userName"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="账号" prop="accountName">
          <el-input
            v-model="addForm.accountName"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            placeholder="默认密码为：123456789" disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="txtGroupsSelect">
          <el-select v-model="addForm.txtGroupsSelect" multiple placeholder="请选择角色">
            <el-option
              v-for="item in roles"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否禁用" prop="locked">
          <el-select v-model="addForm.locked" placeholder="请选择是否禁用">
            <el-option label="是" value="0"></el-option>
            <el-option label="否" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="站点" prop="site">
          <el-select v-model="addForm.site" placeholder="请选择站点">
            <el-option label="*" value="*"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="addForm.description"
            placeholder="请输入账号描述"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="add">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 编辑对话框 -->
    <el-dialog title="编辑用户" v-model="editDialogVisible" width="40%">
      <!-- 对话框内容 -->
      <el-form
        :model="editForm"
        status-icon
        :rules="addFormrules"
        ref="editFormRef"
        label-width="135px"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input
            v-model="editForm.userName"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="账号" prop="accountName">
          <el-input
            v-model="editForm.accountName"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            placeholder="默认密码为：123456789" disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="txtGroupsSelect">
          <el-select v-model="editForm.txtGroupsSelect" multiple placeholder="请选择角色">
            <el-option
              v-for="item in roles"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否禁用" prop="locked">
          <el-select v-model="editForm.locked" placeholder="请选择是否禁用">
            <el-option label="是" value="0"></el-option>
            <el-option label="否" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="站点" prop="site">
          <el-select v-model="editForm.site" placeholder="请选择站点">
            <el-option label="*" value="*"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="editForm.description"
            placeholder="请输入账号描述"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="edit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      userList: [],
      pageCount: 1,
      pageNow: 1,
      pageSize: 10,
      rowCount: 0,
      checkList:[],
      // 显示添加对话框
      addDialogVisible: false,
      // 显示编辑对话框
      editDialogVisible: false,
      addForm: {
        userName: "",
        accountName: "",
        locked: "",
        site:"",
        description: "",
        txtGroupsSelect:[],
      },
      editForm: {},
      addFormrules: {
        userName: [{
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
        ],
        accountName: [
          {
            required: true,
            message: "请输入账号",
            trigger: "blur",
          },
        ],
        locked: [
          {
            required: true,
            message: "请选择是否禁用",
            trigger: "blur",
          },
        ],
        site: [
          {
            required: true,
            message: "请选择站点",
            trigger: "blur",
          },
        ],
        description: [
          {
            required: true,
            message: "请输入账号描述",
            trigger: "blur",
          },
        ],
        txtGroupsSelect:[
          {
            required: true,
            message: "请选择角色",
            trigger: "blur",
          },
        ],
      },
      // 角色选择
      roles:[],
    };
  },
  mounted() {
    this.getList();
    axios.post("/IE_MES/role/findByPage.shtml").then((res) => {
        console.log(res.data.records);
        this.roles = res.data.records;
      });
  },
  methods: {
    getList() {
      let fromdata = new FormData();
      fromdata.append("pageSize", this.pageSize);
      fromdata.append("pageNow", this.pageNow);
      axios.post("/IE_MES/user/findByPage.shtml", fromdata).then((res) => {
        console.log(res.data.records);
        this.userList = res.data.records;
        this.pageCount = res.data.pageCount;
        this.pageNow = res.data.pageNow;
        this.pageSize = res.data.pageSize;
        this.rowCount = res.data.rowCount;
      });
    },
    adduser() {
      console.log(this.roles);
      this.addDialogVisible = true
    },
    // 新增
    add() {
      this.$axios({
            headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
            method: "post",
            url: "/IE_MES/user/addEntity.shtml",
            data: this.$qs.stringify({
              "userFormMap.userName":this.addForm.userName,
              "userFormMap.accountName":this.addForm.accountName,
              "txtGroupsSelect":this.addForm.txtGroupsSelect.toString(),
              "userFormMap.locked":this.addForm.locked,
              "userFormMap.site":this.addForm.site,
              "userFormMap.description":this.addForm.description,
            }),
      }).then((res) => {
            if (res.data == "success") {
              this.getList();
              this.$refs['addFormRef'].resetFields();
              this.addDialogVisible=false;
              this.$message({
                type: "success",
                message: "添加成功!",
              });
            }
          });
    },
     // 编辑
    editDialog(row) {
      this.editForm = JSON.parse(JSON.stringify(row));
      console.log(JSON.parse(JSON.stringify(row)));
      this.editDialogVisible = true;
    },
    edit() {
      this.$axios({
            headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
            method: "post",
            url: "/IE_MES/user/editEntity.shtml",
            data: this.$qs.stringify({
              "userFormMap.id":this.editForm.id,
              "userFormMap.userName":this.editForm.userName,
              "userFormMap.accountName":this.editForm.accountName,
              "txtGroupsSelect":this.editForm.txtGroupsSelect.toString(),
              "userFormMap.locked":this.editForm.locked,
              "userFormMap.site":this.editForm.site,
              "userFormMap.description":this.editForm.description,
            }),
      }).then((res) => {
            if (res.data == "success") {
              this.getList();
              this.editDialogVisible=false;
              this.$message({
                type: "success",
                message: "编辑成功!",
              });
            }
          });
    },
    // 删除
    del(row) {
      this.$confirm("此操作将永久删除该内容, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios({
            headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
            method: "post",
            url: "/IE_MES/user/deleteEntity.shtml?_t="+new Date(),
            data: this.$qs.stringify({
              ids:row.id
            }),
          }).then((res) => {
            if (res.data == "success") {
              this.getList();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.getList();
    },
    handleCurrentChange(newPage) {
      this.pageNow = newPage;
      this.getList();
    },
    formatDate(data) {
      var date = new Date(data);
      var YY = date.getFullYear() + "-";
      var MM =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      var hh =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      var mm =
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        ":";
      var ss =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return YY + MM + DD + " " + hh + mm + ss;
    },
  },
};
</script>

<style lang="less" scoped>
.el-form-item /deep/ .el-input__inner {
  padding-right: 420px !important;
}
</style>
